package cz.muni.fi.pa165.sme.client.panels.burnedCalories;

import cz.muni.fi.pa165.sme.client.converters.ConvertUtils;
import cz.muni.fi.pa165.sme.client.fo.BurnedCaloriesFO;
import cz.muni.fi.pa165.sme.client.fo.SportActivityFO;
import cz.muni.fi.pa165.sme.client.pages.burnedCalories.CaloriesListPage;
import cz.muni.fi.pa165.sme.ws.EndpointManager;
import cz.muni.fi.pa165.sportmanager.burnedcaloriesws.BurnedCalories;
import cz.muni.fi.pa165.sportmanager.burnedcaloriesws.BurnedCaloriesWS;
import cz.muni.fi.pa165.sportmanager.burnedcaloriesws.CreateBurnedCaloriesRequest;
import cz.muni.fi.pa165.sportmanager.burnedcaloriesws.UpdateBurnedCaloriesRequest;
import cz.muni.fi.pa165.sportmanager.sportactivityws.FindAllSportActivityRequest;
import cz.muni.fi.pa165.sportmanager.sportactivityws.SportActivityWS;
import org.apache.wicket.markup.html.form.ChoiceRenderer;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.FormComponent;
import org.apache.wicket.markup.html.form.NumberTextField;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.spring.injection.annot.SpringBean;
import org.springframework.core.convert.ConversionService;

/**
 * @author jirankova
 */
public class CaloriesForm extends Panel {

	@SpringBean(name = "conversionServiceClient")
	private ConversionService conversionService;

	@SpringBean
	private EndpointManager endpointManager;

	private BurnedCaloriesWS burnedCaloriesService;

	private SportActivityWS sportActivityService;

	public CaloriesForm(final String id, final IModel<BurnedCaloriesFO> model) {
		super(id, model);

		burnedCaloriesService = endpointManager.getBurnedCaloriesWS();
		sportActivityService = endpointManager.getSportActivityWS();

		//definition of form components
		final FormComponent<SportActivityFO> sportActivities = new DropDownChoice<SportActivityFO>("sportActivity",
				ConvertUtils.convertList(sportActivityService.findAllSportActivity(new FindAllSportActivityRequest()).getSportActivities(), conversionService,
						SportActivityFO.class), new ChoiceRenderer<SportActivityFO>("title", "id"));
		sportActivities.setRequired(true);

		final FormComponent<Long> calories = new NumberTextField<Long>("calories");
		calories.setRequired(true);
		
		final FormComponent<Long> weight = new NumberTextField<Long>("weight");
		
		final FormComponent<Long> speed = new NumberTextField<Long>("speed");

		//form itself
		final Form<BurnedCaloriesFO> form = new Form<BurnedCaloriesFO>("caloriesForm", model) {
			@Override
			protected void onSubmit() {
				super.onSubmit();
				final BurnedCaloriesFO burnedCaloriesFO = model.getObject();

				final BurnedCalories burnedCaloriesTO = conversionService.convert(burnedCaloriesFO, BurnedCalories.class);

				if(burnedCaloriesTO.getId() != null) {
					final UpdateBurnedCaloriesRequest updateBurnedCaloriesRequest = new UpdateBurnedCaloriesRequest();
					updateBurnedCaloriesRequest.setBurnedCalories(burnedCaloriesTO);
					burnedCaloriesService.updateBurnedCalories(updateBurnedCaloriesRequest);
				} else {
					final CreateBurnedCaloriesRequest createBurnedCaloriesRequest = new CreateBurnedCaloriesRequest();
					createBurnedCaloriesRequest.setBurnedCalories(burnedCaloriesTO);
					burnedCaloriesService.createBurnedCalories(createBurnedCaloriesRequest);
				}
				setResponsePage(CaloriesListPage.class);
			}
		};

		form.add(sportActivities);
		form.add(calories);
		form.add(weight);
		form.add(speed);

		//add form to the panel
		add(form);

		final FeedbackPanel feedbackPanel = new FeedbackPanel("feedbackPanel");
		feedbackPanel.setOutputMarkupId(true);
		add(feedbackPanel);
	}
}
